package com.gym.myshop.dao.impl;

import com.gym.myshop.dao.AddressDao;
import com.gym.myshop.entity.Address;
import com.gym.myshop.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author ming
 * 2021/11/7
 * description:地址
 */
public class AddressDaoImpl implements AddressDao {

    private QueryRunner qr = new QueryRunner();

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:地址显示，降序排序 默认地址为排在第一
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public List<Address> getUserAddressByUid(int uid) {
        try {
            return qr.query(DBUtil.getConnection(),
                    "select * from address where uid = ? order by astate desc",new BeanListHandler<>(Address.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:删除地址 aid
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public int delAddress(Integer aid) {

        try {
            return qr.update(DBUtil.getConnection(),"delete from address where aid = ?",aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:添加地址
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public int insertAddress(Address a) {
        try {
            return qr.update(DBUtil.getConnection(),"insert into address values (default,?,?,?,?,?)",
                    a.getUid(),a.getAname(),a.getAphone(),a.getAdetail(),a.getAstate()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:修改地址
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public int updateAddress(Address a) {
        try {
            return qr.update(DBUtil.getConnection(),
                    "update address set aname=?,aphone=?,adetail=? where aid = ?",
                    a.getAname(),a.getAphone(),a.getAdetail(),a.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:设置默认地址一个，其他都为0，改为非默认 astate = 0
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public int setAllAddress(Integer aid, Integer uid) {

        String sql ="update address set astate = 0 where uid= ? and aid !=?";
        try {
            return qr.update(DBUtil.getConnection(),sql,uid,aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:设置默认地址为1 astate = 1
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    @Override
    public int setAddressDefault(Integer aid) {

        try {
            return qr.update(DBUtil.getConnection(),"update address set astate = 1 where aid = ?",aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
